package io.gitee.zhangbinhub.acp.boot

import io.gitee.zhangbinhub.acp.boot.websocket.reactive.AcpWebSocketServerReactive
import org.springframework.boot.autoconfigure.AutoConfiguration
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication
import org.springframework.context.annotation.Bean
import org.springframework.core.Ordered
import org.springframework.web.reactive.HandlerMapping
import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping
import org.springframework.web.reactive.socket.server.support.WebSocketHandlerAdapter

@AutoConfiguration(after = [AcpComponentAutoConfiguration::class])
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
class AcpWebSocketReactiveAutoConfiguration {
    @Bean
    fun webSocketHandlerAdapter() = WebSocketHandlerAdapter()

    @Bean
    fun handlerMapping(acpWebSocketServerReactiveList: List<AcpWebSocketServerReactive>?): HandlerMapping =
        SimpleUrlHandlerMapping(acpWebSocketServerReactiveList?.associate { Pair(it.getPath(), it) }
            ?: mapOf<String, AcpWebSocketServerReactive>(), Ordered.HIGHEST_PRECEDENCE)
}